import { reqUserCode, reqUserRegister, reqUserToken } from '@/api'

const state = {
    token: '',
    userInfo: {}
}
const mutations = {
    //得到用的token
    RECEIVE_TOKEN(state, token) {
        state.token = token
    },
    //得到用户信息
    receive_userInfo() {

    }
}
const actions = {
    //获取验证码
    async getUserCode({ commit }, phone) {
        const result = await reqUserCode(phone)
        console.log('code', result);
        if (result.code === 200) {
            alert('获取验证码成功')
            return result.data
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    //用户注册
    async getUserRegister({ commit }, userInfo) {
        const result = await reqUserRegister(userInfo)
        if (result.code === 200) {
            return 'ok'
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    //获取用户的token
    async getUserToken({ commit }, user) {
        const result = await reqUserToken(user)
        if (result.code === 200) {
            commit('RECEIVE_TOKEN', result.data.token)
            return 'ok'
        } else {
            return Promise.reject(new Error('failed'))
        }
    },
    //根据用户token获取用户的信息
    async getUserLoginInfo({ commit }) {
        const result = await reqUserLoginInfo()
        if (result.code === 200) {
            commit('')
        }
    }



}
const getters = {}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}